<template>
  <div>
    <el-form label-position="right" :rules="rules" label-width="80px" ref="formLabelAlign" :model="formLabelAlign">
      <el-upload
        class="avatar-uploader"
        action="api/upload"
        :show-file-list="true"
        :on-success="handleAvatarSuccess"
        :before-upload="beforeAvatarUpload">
        <img v-if="imageUrl" :src="imageUrl" class="avatar">
        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
      </el-upload>

      <el-form-item label="店铺名称" prop="sname">
        <el-input v-model="formLabelAlign.sname"></el-input>
      </el-form-item>
      <el-form-item label="商家称谓" prop="username">
        <el-input v-model="formLabelAlign.username"></el-input>
      </el-form-item>
      <el-form-item label="联系方式" prop="phone">
        <el-input v-model="formLabelAlign.phone"></el-input>
      </el-form-item>
      <el-form-item label="店铺地址" prop="address">
        <el-input v-model="formLabelAlign.address"></el-input>
      </el-form-item>
      <el-form-item label="店铺描述" prop="sdesc">
        <el-input v-model="formLabelAlign.sdesc"></el-input>
      </el-form-item>

      <el-button type="danger" @click="add('formLabelAlign')" round>提交</el-button>
      <el-button type="info" @click="main1" round>hello</el-button>
    </el-form>
  </div>
</template>

<script>
  import axios from "axios";
  export default {
    name: "",
    data() {
      return {
        labelPosition: 'right',
        formLabelAlign: {
          userName: '',
          address: '',
          id:'',
          imageurl:'',
        },
        imageUrl: '',

        rules: {
          sname: [
            { required: true, message: '请输入店铺名称', trigger: 'blur' },
            { min: 3, max: 8, message: '长度在 3 到 8 个字符', trigger: 'blur' }
          ],
          username: [
            { required: true, message: '请输入商家称谓', trigger: 'blur' },
            { min: 1, max: 8, message: '长度在 1 到 8 个字符', trigger: 'blur' }
          ],
          phone:[
            {required:true, massage: '请输入联系方式', trigger: 'blur'},
            {min:10, max:12, massage:'手机号码应改为十一位长度的数字', trigger: 'blur'}
          ],
          address: [
            {required:true, massage: '必须填写详细的店铺地址', trigger: 'blur'}
          ]
        }
      };
    },
    mounted(){
      var a =  this.$route.params.id
      //通过id 去后台进行查询数据

      axios.get("/api/findById/"+a).then(res=>{
        this.formLabelAlign=res.data;
      })
      this.formLabelAlign = {"name":"张三","region":"23","type":"123"}
    },
    methods:{

      main1:function () {
        this.$router.push("/main")
      },

      add:function (formName) {
        //验证
        this.$refs[formName].validate((valid) => {
          if (valid) {
            //提交form表单
            axios.post("/api/update",this.formLabelAlign).then(res=>{
              if (res.data==1){
                this.$router.push("/")
              }else {
                alert("修改失败")
              }
            })
          } else {
            console.log('error submit!!');
            return false;
          }
        });
        console.log(this.formLabelAlign)
      },


      handleAvatarSuccess(res, file) {
        this.imageUrl = URL.createObjectURL(file.raw);
        this.formLabelAlign.imageurl=res;
        alert(this.imageUrl)
      },


      beforeAvatarUpload(file) {
        const isJPG = file.type === 'image/jpeg';
        const isLt2M = file.size / 1024 / 1024 < 2;
        if (!isJPG) {
          this.$message.error('上传店铺图片只能是 JPG 格式!');
        }
        if (!isLt2M) {
          this.$message.error('上传店铺图片大小不能超过 2MB!');
        }
        return isJPG && isLt2M;
      }
    }
  }
</script>

<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>

